$( function() {
	$("#select_index").click( function() {
		$("#select_list").toggle();
		return false;
	});
	$("#select_list li").hover( function() {
		$("#select_list li.current").removeClass();
		$(this).attr("class", "current");
	});
	$("#select_list li").click( function() {
		$("#select_index").text($(this).text());
		$("#select_list").hide();
	});
	$("body").click( function() {
		$("#select_list").hide();
	});

	function news() {
		var newsText = $("#news_text");
		newsText.stop(true, true).animate( {
			"top" : "-15px"
		}, 400, function() {
			newsText.append(newsText.find("p:eq(0)"));
			newsText.css("top", "0px");
			t = setTimeout(news, 5000);
		});
	}

	/* 最新发表 滚动*/
	var t = null;
	$("#news_text p").hover( function() {
		clearTimeout(t);
		$("#news_text").stop(true, true);
	}, function() {
		t = setTimeout(news, 2000);
	});

	news();
});

Cms = {};
/**
 * 首页统计
 */
Cms.indexCount = function(base, siteId, checkCount, nocheckCount, todayCount, authorCount) {
	checkCount = checkCount || "checkCount";
	nocheckCount = nocheckCount || "nocheckCount";
	todayCount = todayCount || "todayCount";
	authorCount = authorCount || "authorCount";
	$.getJSON(base + "/index_count.jsp", {
		siteId : siteId
	}, function(data) {
		if (data.length > 0) {
			$("#" + checkCount).text(data[0]);
			$("#" + nocheckCount).text(data[1]);
			$("#" + todayCount).text(data[2]);
			$("#" + authorCount).text(data[3]);
		}
	});
}
/**
 * 浏览次数
 */
Cms.viewCount = function(base, contentId, viewId, commentId, downloadId, upId, downId) {
	viewId = viewId || "views";
	commentId = commentId || "comments";
	downloadId = downloadId || "downloads";
	upId = upId || "ups";
	downId = downId || "downs";
	$.getJSON(base + "/content_view.jsp", {
		contentId : contentId
	}, function(data) {
		if (data.length > 0) {
			$("#" + viewId).text(data[0]);
			$("#" + commentId).text(data[1]);
			$("#" + downloadId).text(data[2]);
			$("#" + upId).text(data[3]);
			$("#" + downId).text(data[4]);
		}
	});
}
/**
 * 成功返回true，失败返回false。
 */
Cms.up = function(base, contentId, origValue, upId) {
	upId = upId || "ups";
	var updown = $.cookie("_cms_updown_" + contentId);
	if (updown) {
		return false;
	}
	$.cookie("_cms_updown_" + contentId, "1");
	$.get(base + "/content_up.jsp", {
		"contentId" : contentId
	}, function(data) {
		$("#" + upId).text(origValue + 1);
	});
	return true;
}
/**
 * 成功返回true，失败返回false。
 */
Cms.down = function(base, contentId, origValue, downId) {
	downId = downId || "downs";
	var updown = $.cookie("_cms_updown_" + contentId);
	if (updown) {
		return false;
	}
	$.cookie("_cms_updown_" + contentId, "1");
	$.get(base + "/content_down.jsp", {
		contentId : contentId
	}, function(data) {
		$("#" + downId).text(origValue + 1);
	});
	return true;
}
/**
 * 获取附件地址
 */
Cms.attachment = function(base, contentId, n, prefix) {
	$.get(base + "/attachment_url.jsp", {
		"cid" : contentId,
		"n" : n
	}, function(data) {
		var url;
		for ( var i = 0; i < n; i++) {
			url = base + "/attachment.jsp?cid=" + contentId + "&i=" + i + data[i];
			$("#" + prefix + i).attr("href", url);
		}
	}, "json");
}
/**
 * 提交评论
 */
Cms.comment = function(callback, form) {
	form = form || "commentForm";
	$("#" + form).validate( {
		submitHandler : function(form) {
			$(form).ajaxSubmit( {
				"success" : callback,
				"dataType" : "json"
			});
		}
	});
}

Cms.reply = function(form) {
	$(form).validate( {
		submitHandler : function() {
			$(form).ajaxSubmit( {
				"success" : function(data) {
					if (data.success) {
						$(form).find("textarea").val("");
						$(form).parents("li").find(".bc_btn_replay").html("回复(" + data.replys + ")");
						var commentId = $(form).find("input[name='commentId']").val();
						Cms.replyList("", $(form).find(".bcr_list"), {
							commentId : commentId
						});
					} else {
						Cms.alertErr("回复", data.status);
					}
				},
				"dataType" : "json"
			});
		}
	});
}

Cms.alertErr = function(type, status) {
	switch (status) {
	case 100:
		alert(type + "对象传递失败，请刷新后再试！");
		break;
	case 101:
		alert(type + "内容不能为空！");
		break;
	case 2:
		alert(type + "对象内容不存在，请刷新后再试！");
		break;
	case 3:
		alert(type + "操作已关闭，请稍候再试！");
		break;
	case 4:
		alert(type + "操作前，请先进行登陆！");
		break;
	case 1:
		alert("验证码输入错误！");
		break;
	default:
		alert("操作失败，错误代码" + status);
		break;
	}
}

/**
 * 获取评论列表
 * 
 * @param siteId
 * @param contentId
 * @param greatTo
 * @param recommend
 * @param orderBy
 * @param count
 */
Cms.commentList = function(base, c, options) {
	c = c || "commentListDiv";
	$("#" + c).load(base + "/comment_list.jsp", options);
}
/**
 * 获取回复列表
 */
Cms.replyList = function(base, c, options) {
	c.load(base + "/reply_list.jsp", options);
}
/**
 * 客户端包含登录
 */
Cms.loginCsi = function(base, c, options) {
	c = c || "loginCsiDiv";
	$("#" + c).load(base + "/login_csi.jsp", options);
}